<template>
	<view class="user-address">
		<view class="no-address column-center" v-if="!hasAccount">
			<image class="img-null mt20" src="/static/images/address_null.png"></image>
			<view class="sm muted">暂无添加地址，请添加~</view>
		</view>
		<view class="address-list" v-else>
			<u-radio-group v-model="radiovalue1" placement="column">
				<view v-for="(item, index) in accountList" :key="index" class="item bg-white mb20 br20"
					:data-id="item.id" :data-zfbAccount="item.zfbAccount" :data-idCardEncrypted="item.idCardEncrypted"
					:data-real_name="item.zfbName" @click="onSelect(item)">
					<view class="address">
						<view class="consignee md bold">
							{{item.receiverName}}
							<text class="phone ml10">{{item.receiverPhone}}</text>
						</view>
						<view class="lighter sm mt10">
							{{item.province}} {{item.city}} {{item.district}} {{item.detailAddress}}
						</view>
					</view>
					<view class="operation row-between">
						<u-radio label="设为默认" :name="item.id" activeColor="#FF2C3C" @change="radioChange(item)">
						</u-radio>
						<!-- 						<text
							style="font-size: 36rpx;font-size: 28rpx;color: #FF2C3C;">{{item.isDefault=='1'? '默认账户':''}}</text> -->
						<text style="font-size: 36rpx;font-size: 28rpx;color: #FF2C3C;"> </text>
						<view class="row-center">
							<view class="row mr20" @click.stop="editAccount(item)">
								<image class="icon-md mr10" src="/static/images/icon_edit.png"></image>
								编辑
							</view>
							<view class="row ml20" :data-id="item.id" @tap.stop="showSurePop(item)">
								<image class="icon-md mr10" src="/static/images/icon_del_1.png"></image>
								删除
							</view>
						</view>
					</view>
				</view>
			</u-radio-group>
		</view>
		<view class="footer row-between fixed bg-white">
			<view class="btn bg-primary white md row-center br60" @click="addAccount">新增收货地址</view>
		</view>
	</view>
</template>

<script>
	import {
		getUserAddressListApi,
		userAddressUpdateApi
	} from '@/api/index.js';
	export default {
		data() {
			return {
				radiovalue1: "",
				accountList: [],
				hasAccount: true,
				deleteSure: false,
				currentId: 0,
				isWeixin: true,
				type: ''
			}
		},
		onLoad: function(options) {
			this.type = options.type;
		},
		onShow: function() {
			this.radiovalue1 = "";
			this.selectBankList();
		},
		methods: {
			radioChange(item) {
				console.log('radioChange', item);
				this.editAccountF({
					province: item.province,
					city: item.city,
					district: item.district,
					detailAddress: item.detailAddress,
					id: item.id,
					receiverName: item.receiverName,
					receiverPhone: item.receiverPhone,
					userId: uni.getStorageSync('UserInfo').userId,
					isDefault: 1,
				})
			},
			// 查询银行列表
			async selectBankList() {
				const res = await getUserAddressListApi(uni.getStorageSync('UserInfo').userId);
				console.log(res, 'res-查询银行列表');
				// radiovalue1

				if (res.code == 200) {
					if (res.data.length) {
						const isDefault = res.data.filter(item => item.isDefault === 1);
						console.log(isDefault, 'isDefault');
						if (isDefault.length > 0) {
							this.radiovalue1 = isDefault[0].id
						}
						this.accountList = res.data;
						this.hasAccount = true;
					} else {
						this.hasAccount = false;
					}
				} else {
					this.hasAccount = false
				}
			},
			onSelect(item) {
				console.log(item, 'item');
				if (this.type) {
					uni.$emit('selectaccount', {
						item
					});
					uni.setStorageSync('user_account', item)
					uni.navigateBack();
				}
			},

			addAccount() {
				uni.setStorageSync('addressInfo', null)
				uni.navigateTo({
					url: '/pages/mine/address/addAddress?type=1'
				});
			},

			editAccount(item) {
				uni.setStorageSync('addressInfo', item)
				uni.navigateTo({
					url: `/pages/mine/address/addAddress`
				});
			},
			// 删除账户
			showSurePop(item) {
				uni.showModal({
					title: '提示',
					content: '您确定要删除该地址吗',
					success: (res) => {
						if (res.confirm) {
							this.editAccountF({
								status: 0,
								userId: item.userId,
								id: item.id,
								isDefault: 0
							});
						}
					}
				})

				// this.deleteSure = true;
				// this.currentId = e.currentTarget.dataset.id;
			},
			// 修改账户
			async editAccountF(params) {
				console.log(params, '请求参数');
				const res = await userAddressUpdateApi(params);
				console.log(res);
				if (res.code === 200) {
					uni.showToast({
						title: res.msg
					})
					this.selectBankList();
				}
			},
			hidePop: function(e) {
				this.deleteSure = false;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.radio-group {
		width: 100%;
	}

	.user-address {
		padding-bottom: calc(140rpx + env(safe-area-inset-bottom));

		.no-address {
			padding-top: 300rpx;
			text-align: center;
		}

		.address-list {
			margin: 20rpx;
			padding: 10rpx 0;

			.item {
				padding: 0 30rpx;

				.address {
					padding: 20rpx 0;
					border-bottom: $-solid-border;
				}

				.operation {
					height: 80rpx;
				}
			}
		}

		.footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			height: 118rpx;
			padding: 0 30rpx;
			box-sizing: content-box;
			padding-bottom: env(safe-area-inset-bottom);

			.btn {
				flex: 1;
				height: 80rpx;
			}
		}
	}

	.tips-dialog {
		height: 230rpx;
		width: 100%;
	}
</style>